package net.entelijan.cobean.examples.showcase.bind.text;

import static org.junit.Assert.assertEquals;
import net.entelijan.cobean.examples.showcase.ShowcaseTestFrame;
import net.entelijan.cobean.examples.util.ExamplesUtil;
import net.entelijan.testutil.CobeanTestUtil;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.fest.swing.fixture.FrameFixture;
import org.fest.swing.fixture.JButtonFixture;
import org.fest.swing.fixture.JTextComponentFixture;
import org.junit.Test;

public class DoubleBindTextFestTestCase {

	@SuppressWarnings("unused")
	private static Log log = LogFactory
			.getLog(DoubleBindTextFestTestCase.class);

	@Test
	public void testSetNameFromGUI() {
		DoubleBindTextPanel panel = new DoubleBindTextPanel();
		CobeanTestUtil.setComponentNamesFromPropertyNames(panel);
		ShowcaseTestFrame frame = new ShowcaseTestFrame(panel);
		FrameFixture frameFix = new FrameFixture(frame);
		try {
			frameFix.show();
			DoubleBindTextShowcase sc = new DoubleBindTextShowcase();
			sc.setService(ExamplesUtil.createTestService());
			sc.init(panel);

			final JTextComponentFixture nameUpTextBox = frameFix
					.textBox("nameUpTextField");
			final JTextComponentFixture nameDownTextBox = frameFix
					.textBox("nameDownTextField");

			nameUpTextBox.deleteText();
			nameUpTextBox.setText("HALLOHALLO");
			nameDownTextBox.focus();
			pause(200);

			assertEquals("HALLOHALLO", sc.getModel().getName());
			nameDownTextBox.requireText("HALLOHALLO");

		} finally {
			frameFix.cleanUp();
		}

	}

	@Test
	public void testSetIdFromGUI() {
		DoubleBindTextPanel panel = new DoubleBindTextPanel();
		CobeanTestUtil.setComponentNamesFromPropertyNames(panel);
		ShowcaseTestFrame frame = new ShowcaseTestFrame(panel);
		FrameFixture frameFix = new FrameFixture(frame);
		try {
			frameFix.show();
			DoubleBindTextShowcase sc = new DoubleBindTextShowcase();
			sc.setService(ExamplesUtil.createTestService());
			sc.init(panel);

			final JTextComponentFixture upTextBox = frameFix
					.textBox("idUpTextField");
			final JTextComponentFixture downTextBox = frameFix
					.textBox("idDownTextField");

			upTextBox.deleteText();
			upTextBox.setText("222");
			downTextBox.focus();
			pause(200);

			assertEquals(222, sc.getModel().getId());
			downTextBox.requireText("222");

		} finally {
			frameFix.cleanUp();
		}

	}

	@Test
	public void testSetNameFromModel() {
		DoubleBindTextPanel panel = new DoubleBindTextPanel();
		CobeanTestUtil.setComponentNamesFromPropertyNames(panel);
		ShowcaseTestFrame frame = new ShowcaseTestFrame(panel);
		FrameFixture frameFix = new FrameFixture(frame);
		try {
			frameFix.show();
			DoubleBindTextShowcase sc = new DoubleBindTextShowcase();
			sc.setService(ExamplesUtil.createTestService());
			sc.init(panel);

			final JTextComponentFixture nameUpTextBox = frameFix
					.textBox("nameUpTextField");
			final JTextComponentFixture nameDownTextBox = frameFix
					.textBox("nameDownTextField");

			sc.getModel().setName("hallo");
			pause(200);

			assertEquals("hallo", sc.getModel().getName());
			nameDownTextBox.requireText("hallo");
			nameUpTextBox.requireText("hallo");

		} finally {
			frameFix.cleanUp();
		}

	}

	@Test
	public void testClickUppercase() {
		DoubleBindTextPanel panel = new DoubleBindTextPanel();
		CobeanTestUtil.setComponentNamesFromPropertyNames(panel);
		ShowcaseTestFrame frame = new ShowcaseTestFrame(panel);
		FrameFixture frameFix = new FrameFixture(frame);
		try {
			frameFix.show();
			DoubleBindTextShowcase sc = new DoubleBindTextShowcase();
			sc.setService(ExamplesUtil.createTestService());
			sc.init(panel);

			final JTextComponentFixture nameUpTextBox = frameFix
					.textBox("nameUpTextField");
			final JTextComponentFixture nameDownTextBox = frameFix
					.textBox("nameDownTextField");
			final JButtonFixture upperButton = frameFix
					.button("uppercaseButton");

			assertEquals("V.S. Naipaul", sc.getModel().getName());
			nameDownTextBox.requireText("V.S. Naipaul");
			nameUpTextBox.requireText("V.S. Naipaul");

			upperButton.click();
			pause(200);

			assertEquals("V.S. NAIPAUL", sc.getModel().getName());
			nameDownTextBox.requireText("V.S. NAIPAUL");
			nameUpTextBox.requireText("V.S. NAIPAUL");

		} finally {
			frameFix.cleanUp();
		}

	}

	private synchronized void pause(int timeMs) {
		try {
			wait(timeMs);
		} catch (InterruptedException e) {
			// Do not mind
		}

	}

}
